package thread;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @Author: Miyako
 * @Date: 2024-07-23-16:40
 * @Description:
 */
public class ThreadPool {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ExecutorService ex = Executors.newFixedThreadPool(3);

        ex.execute(()->{
            System.out.println(Thread.currentThread().getName()+"-1");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        ex.execute(()->{
            System.out.println(Thread.currentThread().getName()+"-2");
        });
        ex.execute(()->{
            System.out.println(Thread.currentThread().getName()+"-3");
        });
        Future<Integer> f = null;
         f =ex.submit(()->{
            Thread.sleep(10000);
           return 1;
        });
        System.out.println(f.get());
        Thread.sleep(100);
        ex.shutdown();//等任务执行完再结束
        ex.shutdownNow();//提前结束

    }
}
